package com.qingxin.sys.action;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;

@Controller
public class NoAuthLoginAction extends BaseAction {
	
	private static class Uinfo{
		private String userid;
		private long time;
		public Uinfo(String userid, long time) {
			this.userid = userid;
			this.time = time;
		}
	}
	
	private static Map<String, Uinfo> uinfoMap = new Hashtable<>();
	
	private static void checkUinfoMap(){
		synchronized (uinfoMap) {
			List<String> keys = new ArrayList<>();
			for(String key : uinfoMap.keySet()){
				Uinfo uinfo = uinfoMap.get(key);
				if(uinfo==null){
					keys.add(key);
				}
				if(System.currentTimeMillis() - uinfo.time > 15000){
					keys.add(key);
				}
			}
			for(String key : keys){
				uinfoMap.remove(key);
			}
		}
	}
	
	
	@RequestMapping(value="/public/getNoauthloginKey.json", method=RequestMethod.POST)
	@ResponseBody
	public Object getNoauthloginKey(String key) {
		try {
			checkUinfoMap();
			String str = new String(Base64.decodeBase64(key), Charset.forName("UTF-8"));
			String[] arr = str.split("\\|");
			String loginName = arr[0];
			long time = Long.parseLong(arr[1]);
			if(Math.abs(System.currentTimeMillis() - time) > 10000){
				return "ERR";
			}
			SysUser sysUser = sysUserService.getByProperty("login_name", loginName);
			if(sysUser == null){
				return "ERR";
			}
			String ukey = StringUtils.UUID();
			Uinfo uinfo = new Uinfo(sysUser.getUserid(), System.currentTimeMillis());
			synchronized (uinfoMap) {
				uinfoMap.put(ukey, uinfo);
			}
			
			String data = ukey + "|" + System.currentTimeMillis();
			return Base64.encodeBase64String(data.getBytes(Charset.forName("UTF-8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ERR";
	}
	
	
	@RequestMapping(value="/public/noauthlogin.do", method=RequestMethod.GET)
	public String noauthlogin(ModelMap model, String key, HttpServletRequest request) {
		try {
			checkUinfoMap();
			String str = new String(Base64.decodeBase64(key), Charset.forName("UTF-8"));
			String[] arr = str.split("\\|");
			String ukey = arr[0];
			long time = Long.parseLong(arr[1]);
			if(Math.abs(System.currentTimeMillis() - time) > 10000){
				return "redirect:/admin/login.do";
			}
			Uinfo uinfo = uinfoMap.get(ukey);
			synchronized (uinfoMap) {
				uinfoMap.remove(ukey);
			}
			if(uinfo == null){
				return "redirect:/admin/login.do";
			}
			
			SysUser sysUser = sysUserService.get(uinfo.userid);
			if(sysUser == null){
				return "redirect:/admin/login.do";
			}
			
			request.getSession().setAttribute("login_kind", "admin");
			request.getSession().setAttribute(SystemCfg.SESSION_LOGIN_USERID, sysUser.getUserid());
			
			return "redirect:/admin/index.do";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/admin/login.do";
	}
	
	
}
